package cn.gpnu.web.servlet;

import cn.gpnu.dao.Userdao;
import cn.gpnu.domain.User;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/Register_serlvet")
public class Register_serlvet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        User RegisterUser = new User();
        try {
            BeanUtils.populate(RegisterUser,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        Userdao dao = new Userdao();
        //判断User
        if(dao.Register(RegisterUser)) {
            req.getRequestDispatcher("/Register_success.html").forward(req,resp);
        }
        else {
            req.getRequestDispatcher("/failServlet").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
